package ldh.im.fxbase.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import javafx.scene.Node;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

public class CacheHelper {

    private Cache<String, Node> nodeCache;

    public CacheHelper(int maxSize) {
        nodeCache = CacheBuilder.newBuilder().maximumSize(maxSize).weigher(this::weight).weakValues().build();
    }

    public void cache(String key, Node node) {
        nodeCache.put(key, node);
    }

    public Node getCache(String key, Callable<Node> callable) throws ExecutionException {
        return nodeCache.get(key, callable);
    }

    private int weight(String key, Node node) {
        return 0;
    }
}
